package org.jeecg.modules.zhoupackage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.degression.mapper.MyUserMapper;
import org.jeecg.modules.degression.pojo.SysUser;
import org.jeecg.modules.zhoupackage.mapper.*;
import org.jeecg.modules.zhoupackage.pojo.*;
import org.jeecg.modules.zhoupackage.service.IMlccFabricInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * 面料Servive
 *
 * @author zxl
 * @since 20200922
 */
@Service
public class MlccFabricInfoServiceImpl implements IMlccFabricInfoService {

    @Autowired
    MlccFabricInfoMapper mapper;

    @Autowired
    MlccFabricTypeMapper fabricTypeMapper;

    @Autowired
    MlccFabricColorMapper colorMapper;

    @Autowired
    MlccGroupInfoMapper groupInfoMapper;

    @Autowired
    MyUserMapper userMapper;

    @Autowired
    MyMlccFabricOrderinfoMapper orderinfoMapper;

    /**
     * 获取面料列表
     *
     * @param entity 查询条件
     * @return
     */
    @Override
    public IPage<MlccFabricInfo> getFabricList(MlccFabricInfo entity, Page<MlccFabricInfo> page) {
        QueryWrapper<MlccFabricInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 0);
        /*对查询条件的封装*/
        if (entity != null) {
            if (entity.getFabricCode() != null && entity.getFabricCode() != "") {
                wrapper.like("fabric_code", entity.getFabricCode());
            }
            if (entity.getFabricTypeId() != null && entity.getFabricTypeId() != "") {
                wrapper.eq("fabric_type_id", entity.getFabricTypeId());
            }
            if (entity.getFabricColorId() != null && entity.getFabricColorId() != "") {
                wrapper.eq("fabric_color_id", entity.getFabricColorId());
            }
        }
        wrapper.ne("fabric_code", "-999999");
        IPage<MlccFabricInfo> iPage = mapper.selectPage(page, wrapper);
        List<MlccFabricInfo> fabricInfos = iPage.getRecords();
        // 判断面料对应的面料组是否存在，如果不存在，则不显示这个面料
        Iterator<MlccFabricInfo> iterator = fabricInfos.iterator();
        while (iterator.hasNext()) {
            MlccFabricInfo next = iterator.next();
            if (next.getGroupId() != null) {
                QueryWrapper<MlccGroupInfo> groupInfoQueryWrapper = new QueryWrapper<>();
                groupInfoQueryWrapper.eq("status", 0).eq("id", next.getGroupId());
                MlccGroupInfo groupInfo = groupInfoMapper.selectOne(groupInfoQueryWrapper);
                if (groupInfo == null) {
                    iterator.remove();
                }
            }
        }
        return iPage;
    }

    /**
     * 面料下单页面获取面料类型
     *
     * @return
     */
    @Override
    public List<MlccFabricType> getFabricType() {
        return fabricTypeMapper.selectList(null);
    }

    /**
     * 获取面料颜色数据
     *
     * @return
     */
    @Override
    public List<MlccFabricColor> getColorList() {
        return colorMapper.selectList(null);
    }

    /**
     * 根据面料ID获取面料详情
     *
     * @param id
     * @return
     */
    @Override
    public MlccFabricInfo getFabricDetailByID(int id) {
        MlccFabricInfo fabricInfo = mapper.selectById(id);
        if (fabricInfo != null && fabricInfo.getGroupId() != null) {
            fabricInfo.setFabricPrice(groupInfoMapper.selectById(fabricInfo.getGroupId()).getPrice());
        }
        String sysuserId = fabricInfo.getSysuserId();
        if (sysuserId != null && !"".equals(sysuserId)) {
            SysUser user = userMapper.selectById(sysuserId);
            if (user != null) {
                fabricInfo.setGysCode(user.getOrgCode());
            }
        }
        return fabricInfo;
    }

    /**
     * 编辑面料订单备注
     *
     * @param orderSn 订单编号
     * @param remark  备注信息
     */
    @Override
    public int updateFabricRemark(String orderSn, String remark) {
        QueryWrapper<MlccFabricOrderinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_sn", orderSn);
        MlccFabricOrderinfo one = orderinfoMapper.selectOne(wrapper);
        if (one != null) {
            one.setRemark(remark);
            return orderinfoMapper.updateById(one);
        }
        return 0;
    }
}
